const {createItems, getItemsInfo, updateItemsInfo} = require('../service/items.service');

const {createLog} = require('../service/log.service');

const {databaseError} = require('../constant/err.type');

class ItemsController {

    async addItems(ctx, next) {
        let {itemId, itemName, itemDesc, price, itemType, pileNumber, itemPath, relationId, relationType, foodHP, foodEP, foodBuff, composition} = ctx.request.body;
        // 操作数据库
        try {
            let res = await createItems(itemId, itemName, itemDesc, price, itemType, pileNumber, itemPath, relationId, relationType, foodHP, foodEP, foodBuff, composition);
            // 记录日志
            await createLog(ctx, `添加物品成功，物品名：${itemName}`);
            ctx.body = {
                code: 1,
                message: '物品添加成功！',
                result: {
                    id: res.id,
                    itemName: res.itemName
                }
            };
        } catch (e) {
            ctx.app.emit('error', databaseError, ctx)
        }
    }

    async updateItems(ctx, next) {
        // 获取数据
        let {id, itemId, itemName, itemDesc, price, itemType, pileNumber, itemPath, relationId, relationType, foodHP, foodEP, foodBuff, composition} = ctx.request.body;
        // 操作数据库
        try {
            let res = await updateItemsInfo({
                id,
                itemId,
                itemName,
                itemDesc,
                price,
                itemType,
                pileNumber,
                itemPath, relationId, relationType, foodHP, foodEP, foodBuff,
                composition
            });
            if (res) {
                // 记录日志
                await createLog(ctx, `修改物品成功，物品名：${itemName}`);
                ctx.body = {
                    code: 1,
                    message: '物品修改成功！',
                    result: {}
                }
            } else {
                ctx.app.emit('error', databaseError, ctx);
            }
        } catch (e) {
            ctx.app.emit('error', databaseError, ctx);
        }
    }

    async queryItems(ctx, next) {

        let {id, itemName, itemType, itemDesc, flag, pageInfo} = ctx.request.query;
        try {
            let res = await getItemsInfo({id, itemName, itemType, itemDesc, flag, pageInfo});
            ctx.body = {
                code: 1,
                message: '物品查询成功！',
                data: res
            }

        } catch (e) {
            ctx.app.emit('error', databaseError, ctx);
        }

    }

    async queryItemsDetail(ctx, next) {
        let {id} = ctx.request.query;
        try {
            let res = await getItemsInfo({id});
            ctx.body = {
                code: 1,
                message: '物品详情查询成功！',
                result: res
            }

        } catch (e) {
            ctx.app.emit('error', databaseError, ctx);
        }

    }

    async deleteItems(ctx, next) {
        // 获取数据
        let {id, flag} = ctx.request.body;
        // 操作数据库
        try {
            let res = await updateItemsInfo({id, flag});
            if (res) {
                // 记录日志
                await createLog(ctx, `删除物品成功，物品ID:${id}`);
                ctx.body = {
                    code: 1,
                    message: '物品删除成功！',
                    result: {}
                }
            } else {
                ctx.app.emit('error', databaseError, ctx);
            }
        } catch (e) {
            ctx.app.emit('error', databaseError, ctx);
        }
    }
}

module.exports = new ItemsController();